package com.lzq.springboot_lzq.service;


import com.lzq.springboot_lzq.Repository.DeptRepository;
import com.lzq.springboot_lzq.pojo.Dept;
import com.lzq.springboot_lzq.pojo.dto.DeptDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeptService implements IDeptService {

    @Autowired
    DeptRepository deptRepository;

    @Override
    public Dept add(DeptDto dept) {
        Dept DeptNew = new Dept();
        BeanUtils.copyProperties(dept,DeptNew);
        return deptRepository.save(DeptNew);
        //调用数据访问
    }

    @Override
    public Dept findById(int id) {
        return deptRepository.findById(id).orElseThrow( () -> {
            throw new IllegalArgumentException("用户不存在");
        });
    }

    @Override
    public Dept delete(int id) {
        deptRepository.deleteById(id);
        return null;
    }

    @Override
    public Iterable<Dept> findAll() {
        return deptRepository.findAll();
    }

    @Override
    public Dept edit(DeptDto dept) {
        Dept deptnew = new Dept();
        BeanUtils.copyProperties(dept,deptnew);
        return deptRepository.save(deptnew);
    }
}
